package aglieBpm.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

public class DataBaseUtil {

	public static JSONArray excuteSql(String url , String username , String password , String sql) {
		JSONArray jsonArray = new JSONArray() ;
		try {
			Connection conn = DriverManager.getConnection(url, username, password);
			Statement statement = conn.createStatement();
			ResultSet rs = statement.executeQuery(sql) ;
			
			while(rs.next()) {
				JSONObject jsonObject = new JSONObject() ;
				ResultSetMetaData metaData = rs.getMetaData() ;
				int	columnCount	=	metaData.getColumnCount(); 
				for(int i = 1 ; i <= columnCount ; i++) {
					String name	= metaData.getColumnLabel(i) ;
					String value = rs.getString(name) ;
					if(value==null) value = "" ;
					jsonObject.put(name, value) ;
				}
				jsonArray.add(jsonObject) ;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
				                        
		return jsonArray ;
	}
	
}
